package ch.ethz.inf.shop.model;

import java.sql.ResultSet;
import java.sql.SQLException;

import ch.ethz.inf.shop.Title;
import ch.ethz.inf.shop.model.simpleDatabase.Tuple;

/**
 * Object that represents a registered in user.
 */
public final class RegisteredUser {

	@Title(text = "User", isVisible = true)
	private final String name;
	@Title(text = "Password", isVisible = false)
	private final String password;
	@Title(text = "Email", isVisible = true)
	private final String email;
	@Title(text = "User ID", isVisible = false)
	private final int userId;

	@Override
	public String toString() {
		return name;
	}

	public RegisteredUser(final String name, final String password, final String email, final int user_id) {

		this.name = name;
		this.password = password;
		this.email = email;
		this.userId = user_id;
	}

	public RegisteredUser(ResultSet rs) throws SQLException {
		this.name = rs.getString("name");
		this.userId = rs.getInt("user_id");
		this.email = rs.getString("email");
		this.password = rs.getString("passwd");
	}

	public RegisteredUser(Tuple tuple) {
		this.name = tuple.getString("registered_user.name");
		this.userId = tuple.getInt("registered_user.user_id");
		this.email = tuple.getString("registered_user.email");
		this.password = tuple.getString("registered_user.passwd");
	}

	public String getPassword() {
		return password;
	}

	public String getEmail() {
		return email;
	}

	public int getUserId() {
		return userId;
	}

	public String getName() {
		return name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + userId;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RegisteredUser other = (RegisteredUser) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (userId != other.userId)
			return false;
		return true;
	}

}
